#include "YUVFaceJudge.h"
#ifdef opencvdebug
cv::Mat YUVFaceJudgemine::GetYImgCV(std::string yuvpath, int width, int height)
{
	std::vector<unsigned char> yuv = readYUVText(yuvpath, width, height);
	cv::Mat image = cv::Mat::zeros(width, height, CV_8UC1);
	for (int row = 0; row < image.rows; row++)
	{
		for (int col = 0; col < image.cols; col++)
		{
			int i = col * image.rows + row;
			image.at<unsigned char>(row, col) = yuv.at(i);
		}
	}
	cv::Mat image2 = cv::Mat::zeros(height, width, CV_8UC1);
	for (int row = 0; row < image2.rows; row++)
	{
		for (int col = 0; col < image2.cols; col++)
		{
			int i = row * image2.cols + col;
			image2.at<unsigned char>(row, col) = yuv.at(i);
		}
	}
	return image;
}
#endif

std::vector<unsigned char> YUVFaceJudgemine::GetYImg(std::string yuvpath, int width, int height)
{
	std::vector<unsigned char> y_channel = readYUVText(yuvpath, width, height);
	std::vector<unsigned char> y_channel_rotate(y_channel.size());
	for (int row = 0; row < width; row++)
	{
		for (int col = 0; col < height; col++)
		{
			int i = col * width + row;
			int j = row * height + col;
			y_channel_rotate.at(j) = y_channel.at(i);
		}
	}
	//cv::Mat imageshow = cv::Mat::zeros(width, height, CV_8UC1);
	//for (int row = 0; row < imageshow.rows; row++)
	//{
	//	for (int col = 0; col < imageshow.cols; col++)
	//	{
	//		int i = row * height + col;
	//		imageshow.at<signed char>(row, col) = y_channel_rotate.at(i);
	//	}
	//}
	return y_channel_rotate;
}

std::vector<unsigned char> YUVFaceJudgemine::GetBGRImg(std::string yuvpath, int width, int height)
{
	std::vector<unsigned char> y_channel = readYUVText(yuvpath, width, height);
	std::vector<unsigned char> y_channel_rotate(y_channel.size() * 3);
	for (int row = 0; row < width; row++)
	{
		for (int col = 0; col < height; col++)
		{
			int i = col * width + row;
			int j = row * height + col;
			y_channel_rotate.at(3 * j + 0) = y_channel.at(i);
			y_channel_rotate.at(3 * j + 1) = y_channel.at(i);
			y_channel_rotate.at(3 * j + 2) = y_channel.at(i);
		}
	}
	//cv::Mat imageshow = cv::Mat::zeros(width, height, CV_8UC3);
	//for (int row = 0; row < imageshow.rows; row++)
	//{
	//	for (int col = 0; col < imageshow.cols; col++)
	//	{
	//		int i = row * height + col;
	//		imageshow.at<cv::Vec3b>(row, col)[0] = y_channel_rotate.at(3 * i + 0);
	//		imageshow.at<cv::Vec3b>(row, col)[1] = y_channel_rotate.at(3 * i + 1);
	//		imageshow.at<cv::Vec3b>(row, col)[2] = y_channel_rotate.at(3 * i + 2);
	//	}
	//}
	return y_channel_rotate;
}

std::vector<unsigned char> YUVFaceJudgemine::GetBGRAImg(std::string yuvpath, int width, int height)
{
	std::vector<unsigned char> y_channel = readYUVText(yuvpath, width, height);
	std::vector<unsigned char> y_channel_rotate(y_channel.size() * 4);
	for (int row = 0; row < width; row++)
	{
		for (int col = 0; col < height; col++)
		{
			int i = col * width + row;
			int j = row * height + col;
			y_channel_rotate.at(4 * j + 0) = y_channel.at(i);
			y_channel_rotate.at(4 * j + 1) = y_channel.at(i);
			y_channel_rotate.at(4 * j + 2) = y_channel.at(i);
			y_channel_rotate.at(4 * j + 3) = 255;
		}
	}
	//cv::Mat imageshow = cv::Mat::zeros(width, height, CV_8UC4);
	//for (int row = 0; row < imageshow.rows; row++)
	//{
	//	for (int col = 0; col < imageshow.cols; col++)
	//	{
	//		int i = row * height + col;
	//		imageshow.at<cv::Vec4b>(row, col)[0] = y_channel_rotate.at(4 * i + 0);
	//		imageshow.at<cv::Vec4b>(row, col)[1] = y_channel_rotate.at(4 * i + 1);
	//		imageshow.at<cv::Vec4b>(row, col)[2] = y_channel_rotate.at(4 * i + 2);
	//		imageshow.at<cv::Vec4b>(row, col)[3] = y_channel_rotate.at(4 * i + 3);
	//	}
	//}
	return y_channel_rotate;
}

std::vector<unsigned char> YUVFaceJudgemine::readYUVText(std::string yuvpath, int width, int height)
{
	std::vector<unsigned char> raw_yuv_vec(width*height);
	std::ifstream ifs(yuvpath, std::ios::binary | std::ios::in);
	ifs.read((char*)&raw_yuv_vec[0], raw_yuv_vec.size());
	return raw_yuv_vec;
}

YUVFaceJudgemine::YUVFaceJudgemine()
{
}

YUVFaceJudgemine::~YUVFaceJudgemine()
{
}